import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/get_navigation.dart';
import 'package:oktoast/oktoast.dart';
import 'color_utils.dart';
import 'common_utils.dart';

class Loading {
  static bool isShow = false;

  static showLoading() {
    if (!isShow) {
      // isShow = true;
      // FocusManager.instance.primaryFocus?.unfocus();
      // Navigator.push(
      //     Get.context!,
      //     _PopRoute(
      //         child: WillPopScope(
      //       onWillPop: () {
      //         return Future.value(false);
      //       },
      //       child: Material(
      //         color: Colors.transparent,
      //         child: Center(
      //           child: Container(
      //             width: 100,
      //             height: 100,
      //             decoration: const BoxDecoration(
      //               color: Colors.black54,
      //               borderRadius: BorderRadius.all(Radius.circular(10)),
      //             ),
      //             child: Theme(
      //               data: ThemeData(
      //                 cupertinoOverrideTheme: const CupertinoThemeData(
      //                   brightness: Brightness.dark,
      //                 ),
      //               ),
      //               child: const CupertinoActivityIndicator(
      //                 radius: 14,
      //               ),
      //             ),
      //           ),
      //         ),
      //       ),
      //     )));

      isShow = true;
      FocusManager.instance.primaryFocus?.unfocus();
      showDialog(
          context: Get.context!,
          builder: (context) {
            return WillPopScope(
              onWillPop: () {
                return Future.value(false);
              },
              child: Align(
                child: Container(
                  width: 100,
                  height: 100,
                  decoration: const BoxDecoration(
                    color: Colors.black54,
                    borderRadius: BorderRadius.all(Radius.circular(10)),
                  ),
                  child: Theme(
                    data: ThemeData(
                      cupertinoOverrideTheme: const CupertinoThemeData(
                        brightness: Brightness.dark,
                      ),
                    ),
                    child: const CupertinoActivityIndicator(
                      radius: 14,
                    ),
                  ),
                ),
              ),
            );
          });
    }
  }

  static hideLoading() {
    if (isShow) {
      if (Navigator.of(Get.context!).canPop()) {
        Navigator.pop(Get.context!);
        isShow = false;
      }
    }
  }
}

///Route
class _PopRoute extends PopupRoute {
  final Duration _duration = const Duration(milliseconds: 300);
  Widget child;

  _PopRoute({required this.child});

  @override
  Color? get barrierColor => null;

  @override
  bool get barrierDismissible => true;

  @override
  String? get barrierLabel => null;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return child;
  }

  @override
  Duration get transitionDuration => _duration;
}

void xdShowToast(String title, {int durationTime = 2}) {
  if (title.isEmpty) {
    return;
  }
  showToast(
    title,
    position: ToastPosition.center,
    textStyle: const TextStyle(
        fontSize: 14,
        color: ThemeColors.clffffffff,
        fontWeight: FontWeight.normal,
        decoration: TextDecoration.none),
    textPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
    radius: 10.0,
    duration: Duration(seconds: durationTime),
    backgroundColor: ThemeColors.cl80000000,
  );
}